package components;

import javax.swing.*;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.BadLocationException;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.net.URL;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: admin
 * Date: 23.01.2006
 * Time: 13:10:20
 * To change this template use File | Settings | File Templates.
 */
public class WMLEditorPane extends JTextPane implements FocusListener {
        public void setCardID(String cardID) {
            this.cardID = cardID;
        }

        String cardID;

        public void setCardTitle(String title) {
            this.title = title;
        }

    String title;
    public WMLEditorPane(String cardId) {
        super();
        cardID = cardId;
        StyleSheet styleSheet = new StyleSheet();
        URL cssURL = getClass().getClassLoader().getResource("ekit.css");
        styleSheet.importStyleSheet(cssURL);

        HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
        htmlEditorKit.setStyleSheet(styleSheet);
        htmlEditorKit.install(this);


        setEditorKit(htmlEditorKit);
        setMaximumSize(new Dimension(250,400));
        setText("<p>Simple page</p>");
        addFocusListener(this);
    }


     public WMLEditorPane(String cardId,String title) {
        super();
        cardID =cardId;
        this.title=title;
        StyleSheet styleSheet = new StyleSheet();
        URL cssURL = getClass().getClassLoader().getResource("ekit.css");

        styleSheet.importStyleSheet(cssURL);

        HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
        htmlEditorKit.setStyleSheet(styleSheet);
        htmlEditorKit.install(this);

        getInputMap().put(KeyStroke.getKeyStroke("ctrl C"), "bogus");
        getInputMap().put(KeyStroke.getKeyStroke("ctrl V"), "bogus");
        getInputMap().put(KeyStroke.getKeyStroke("ctrl X"), "bogus");
        setEditorKit(htmlEditorKit);
        setMaximumSize(new Dimension(250,400));
      // setText("<form id=\""+cardId+"\" title=\""+title+"\"><p><b>Simple</b> page</p></form>");
         setText("<p>Simple page</p>");
         addFocusListener(this);
    }
     public WMLEditorPane(String cardId,String text,String title) {
        super();
        cardID =cardId;
        this.title = title;
        StyleSheet styleSheet = new StyleSheet();
        URL cssURL = getClass().getClassLoader().getResource("ekit.css");
        styleSheet.importStyleSheet(cssURL);

        HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
        htmlEditorKit.setStyleSheet(styleSheet);
        htmlEditorKit.install(this);

        getInputMap().put(KeyStroke.getKeyStroke("ctrl C"), "bogus");
        getInputMap().put(KeyStroke.getKeyStroke("ctrl V"), "bogus");
        getInputMap().put(KeyStroke.getKeyStroke("ctrl X"), "bogus");
        setEditorKit(htmlEditorKit);
        setMaximumSize(new Dimension(250,400));
        setText(text);
        addFocusListener(this);
     }
   public String getCardID()
   {

        return cardID;
   }
    public String getCardTitle()
    {

        return title;
    }

    public void focusGained(FocusEvent e) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public void focusLost(FocusEvent e) {


        //To change body of implemented methods use File | Settings | File Templates.
    }

    protected void insertHTMLTag(String content, HTML.Tag tag) throws BadLocationException, IOException {
        int currentPosition = getCaretPosition();
        HTMLEditorKit htmlEditorKit = (HTMLEditorKit) getEditorKit();
        htmlEditorKit.insertHTML((HTMLDocument) getDocument(), currentPosition, content, 0, 0, tag);
        requestFocus();
    }
}
